package com.applet.applet_shangcheng.controller;

import com.applet.applet_shangcheng.entity.Announcement;
import com.applet.applet_shangcheng.service.AnnouncementService;
import com.applet.applet_shangcheng.utlis.PageResult;
import com.applet.applet_shangcheng.utlis.QueryPageBean;
import com.applet.applet_shangcheng.utlis.Result;
import com.applet.applet_shangcheng.utlis.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/announcement")
@CrossOrigin
@Api(value="公告管理",tags={"公告管理"})
public class AnnouncementController  {

    @Autowired
    private AnnouncementService announcementService;
    //查询所有有效状态


    @ApiOperation(value="WX查询公告成功")
    @PostMapping("/WXfindAllS")
    public Result findAllS() {
        List<Announcement> announcementList = announcementService.findAllS();
        return new Result(true, StatusCode.OK,"查询成功公告成功",announcementList);
    }


    //管理
    @ApiOperation(value="_查询公告")
    @PostMapping("/findAll")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = announcementService.findAll(queryPageBean.getQueryString(),queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        return pageResult;
    }

    //查询所有
    @ApiOperation(value="_根据id查询公告")
    @GetMapping("/findAnnouncement")
    public Result findAnnouncement(@RequestParam("id") int id){
        Announcement announcementList = announcementService.findAnnouncement(id);
        return new Result(true, StatusCode.OK,"查询成功公告成功",announcementList);
    }

    //添加
    @ApiOperation(value="_添加公告")
    @PostMapping("/insertAnnouncement")
    public Result insertAnnouncement(@RequestBody Announcement announcement){
        String content = announcement.getContent();
        if (content==null){
            return new Result(false, StatusCode.ERROR,"公告内容不能为空");
        }
        int result = announcementService.insertAnnouncement(announcement);
        if (result>0){
            return new Result(true, StatusCode.OK,"添加公告成功");
        }
        return new Result(false, StatusCode.ERROR,"添加公告失败");
    }
    //修改状态,标记删除
    @ApiOperation(value="_根据id修改公告状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable(value = "id") int id,
                               @PathVariable(value = "status") int status){
        int result = announcementService.updateStatus(id,status);
        if (result>0){
            return new Result(true, StatusCode.OK,"修改公告状态成功");
        }
        return new Result(false, StatusCode.ERROR,"修改公告状态失败");
    }
    //删除
    @ApiOperation(value="_删除公告")
    @GetMapping("/deleteAnnouncement/{id}")
    public Result deleteAnnouncement(@PathVariable(value = "id") int id){
        int result = announcementService.deleteAnnouncement(id);
        if (result>0){
            return new Result(true, StatusCode.OK,"删除公告成功");
        }
        return new Result(false, StatusCode.ERROR,"删除公告失败");
    }
    //修改
    @ApiOperation(value="_修改公告")
    @PostMapping("/updateAnnouncement")
    public Result updateAnnouncement(@RequestBody Announcement announcement){
        int result = announcementService.updateAnnouncement(announcement);
        if (result>0){
            return new Result(true, StatusCode.OK,"修改公告成功");
        }
        return new Result(false, StatusCode.ERROR,"修改公告失败");
    }
}
